package com.csu.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csu.mall.common.utils.PageUtils;
import com.csu.mall.common.utils.Query;
import com.csu.mall.product.dao.AttrAttrgroupRelationDao;
import com.csu.mall.product.dao.AttrDao;
import com.csu.mall.product.dao.AttrGroupDao;
import com.csu.mall.product.entity.AttrAttrgroupRelationEntity;
import com.csu.mall.product.entity.AttrEntity;
import com.csu.mall.product.entity.AttrGroupEntity;
import com.csu.mall.product.service.AttrAttrgroupRelationService;
import com.csu.mall.product.service.AttrGroupService;
import com.csu.mall.product.service.CategoryService;
import com.csu.mall.product.vo.AttrGroupVo;
import com.csu.mall.product.vo.AttrResVo;
import com.csu.mall.product.vo.AttrVo;
import com.csu.mall.product.vo.SpuItemAttrGroupVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
@Slf4j
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Resource
    private AttrGroupDao attrGroupDao;

    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrDao attrDao;


    @Resource
    private AttrAttrgroupRelationDao relationDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(long catelogId, Map<String, Object> params) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if(catelogId != 0){
            wrapper.eq("catelog_id", catelogId);
        }
        String key = (String) params.get("key");
        if(StringUtils.isNotEmpty(key)){
            wrapper.and(item->{
                item.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

    @Override
    public AttrGroupEntity getAttrGroup(Long attrGroupId) {
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);

        Long[] catelogPath = categoryService.getCatelogPath(attrGroupEntity.getCatelogId());
        attrGroupEntity.setCatelogPath(catelogPath);
        return attrGroupEntity;
    }

    /**
     * 获取属性分组所有的属性
     */
    @Override
    public List<AttrEntity> getAttrsOfAttrGroup(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<Long> attrIds = relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        if(attrIds.size() > 0){
            return attrDao.batchSelectAttrEntity(attrIds);
        }
        return null;
    }

    @Override
    public void deleteAttrsOfAttrGroup(List<AttrVo> attrVos) {
        if(attrVos != null && attrVos.size() > 0){
            AttrVo attrVo = attrVos.get(0);
            attrGroupDao.deleteAttrAttrGroupRelation(attrVo.getAttrId(), attrVo.getAttrGroupId());
        }
    }

    @Override
    public PageUtils selectEnableAttr(Long attrgroupId) {
        IPage<AttrEntity> page = new Page<>();
        List<AttrEntity> attrEntities = attrGroupDao.selectEnableAttr(attrgroupId);
        page.setRecords(attrEntities);
        return new PageUtils(page);
    }

    @Override
    public void saveBatchAttrAttrGroup(List<AttrVo> attrVos) {
        List<AttrAttrgroupRelationEntity> relationEntities = new ArrayList<>();
        for(AttrVo attrVo: attrVos){
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(attrVo.getAttrId());
            relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
            relationEntities.add(relationEntity);
        }
        relationDao.batchInsertEntity(relationEntities);
    }

    @Override
    public List<AttrGroupVo> getAttrGroupAndAttr(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.getAttrGroup(catelogId);
        List<AttrGroupVo> resList = new ArrayList<>();
        for (AttrGroupEntity attrGroupEntity : attrGroupEntities) {
            attrGroupEntity.getAttrGroupId();
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            Long attrGroupId = attrGroupEntity.getAttrGroupId();
            if(attrGroupId != null && attrGroupId > 0){
                List<AttrVo> attrVoList = attrDao.selectAttrVoList(attrGroupEntity.getAttrGroupId());
                attrGroupVo.setAttrs(attrVoList);
            }

            BeanUtils.copyProperties(attrGroupEntity, attrGroupVo);
            resList.add(attrGroupVo);
        }
        return resList;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {

        //1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);

        return vos;
    }


}